package com.tech.koufu.ui.activity.menu;

import com.tech.koufu.MyApplication;
import com.tech.koufu.R;
import com.tech.koufu.model.UserInfo;
import com.tech.koufu.model.UserStocks;
import com.tech.koufu.ui.activity.DetailTransStocksActivity;
import com.tech.koufu.ui.adapter.PostionsStocksAdapter;
import com.tech.koufu.ui.adapter.TradeAdapter;

import extras.com.handmark.pulltorefresh.library.PullToRefreshListView;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
/**
 * @ClassName: MyPositionsActivity
 * @Description: 菜单->我的仓位  
 * @author jl_luo
 * @date 2015年4月5日 下午2:02:15
 */
public class MyPositionsActivity extends Activity implements OnClickListener{
	private static final String TAG = "cf_c";
	private MyApplication myApp;
	private PullToRefreshListView mPullToRefreshListView;
	private ListView actualListView;
	private PostionsStocksAdapter mAdapter; 
	private ImageView icHead;
	private TextView  txtUserName,txt_positions_value;
	private String userid;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu_my_positions);
		myApp = (MyApplication) getApplication();
		initView();
	}
	private void initView(){
		userid = getIntent().getStringExtra("userid");
		findViewById(R.id.img_callback).setOnClickListener(this);
		TextView title = ((TextView)findViewById(R.id.tv_title));
		if(userid!=null && myApp.getUserid().equals(userid)){
			title.setText(R.string.txt_main_mycc);
		}else{
			title.setText(R.string.txt_cc);
		}
		icHead = (ImageView)findViewById(R.id.ic_head);
		txtUserName = (TextView)findViewById(R.id.user_name);
		txt_positions_value = (TextView)findViewById(R.id.txt_positions_value);
		
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.lv_pull);
		actualListView = mPullToRefreshListView.getRefreshableView();
		registerForContextMenu(actualListView);

		mAdapter = new PostionsStocksAdapter(getApplicationContext());
		actualListView.setAdapter(mAdapter);
		actualListView.setOnItemClickListener(itemClick); 
		showData();
	}
	private void showData(){
		if(myApp.isLogin){
			txtUserName.setText(myApp.getUserName());
			mAdapter.mStockInfos = myApp.mStockInfos;
			mAdapter.mUserStocks = myApp.mUserStocks;
			mAdapter.notifyDataSetChanged();
		} 
		UserInfo info = myApp.mUserInfo;
		String c;
		if(info.getCw().equals("空仓")){
			c = ("0.00%");
		}else {
			c = (info.getCw());
		} 
        c = String.format(getResources().getString(R.string.txt_positions_value), c);
        SpannableString style=new SpannableString(c);          
        style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.bgBlackColor_63)),0,3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
        txt_positions_value.setText(style);   
        
	}
	private OnItemClickListener itemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {  
			Intent intent = new Intent(MyPositionsActivity.this,DetailTransStocksActivity.class); 
			intent.putExtra("userStock", (UserStocks)myApp.mUserStocks.get(position-1)); 
			startActivity(intent);
		}
	};

	@Override
	protected void onResume() { 
		super.onResume();
	}

	@Override
	public void onClick(View v) { 
		switch (v.getId()) {
		case R.id.img_callback:
			this.finish();
			break;

		default:
			break;
		}
	}
}
